package com.custom.lessonstudy.net;

import android.net.ParseException;
import com.alibaba.fastjson.JSONObject;

import com.custom.lessonstudy.di.HttpInterceptor;
import com.google.gson.JsonParseException;


import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;



/**
 * Created by cyb on 2018/11/5.
 * 这是用来解析返回数据的,基于io.reactivex.Observer
 */

 public abstract class HttpResponseObserver<T> implements Observer<T> {

    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;


    @Override
    public void onError(Throwable e) {
        String errorStr = "";
        //数据解析异常
//       if (e instanceof Exception) {
//           errorStr = e.getMessage();
//       }
//
        if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            switch (httpException.code()) {
                case UNAUTHORIZED:
                case FORBIDDEN:
                case NOT_FOUND:
                case REQUEST_TIMEOUT:
                case GATEWAY_TIMEOUT:
                case INTERNAL_SERVER_ERROR:
                case BAD_GATEWAY:
                case SERVICE_UNAVAILABLE:
                default:
                    errorStr = "网络错误,请检查网络并重试";
                    break;
            }
            error(-1,errorStr);
        } else if (e instanceof RuntimeException) {
            errorStr = "服务器异常，请稍后重试";
        } else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {

            errorStr = "解析错误，请重试";
        } else if (e instanceof ConnectException) {
            errorStr = "连接失败，请检查网络并重试";
        }else if(e instanceof SocketTimeoutException){
            errorStr = "连接超时，请检查网络并重试";
        } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
            errorStr = "证书验证失败，请重试";
        }else if(e instanceof HttpException){
            errorStr = "服务器异常，请重试";
        } else {
            errorStr = "请求失败，请检查网络并重试";
        }

        error(-1,errorStr);
    }

    @Override
    public void onNext(T data) {
        try {

            String requestParameter = HttpInterceptor.getRequestParameter();
            String responseParameter = HttpInterceptor.getResponseParameter();
//            Log.d("LUO","========"+requestParameter);
//            Log.d("LUO","========"+responseParameter);
            if (data instanceof BaseResponse) {
                //基础数据
                BaseResponse baseResponse = (BaseResponse) data;
                //对于所有的网络请求，1-成功 : -0,失败
                int state = baseResponse.getResponseState();
                // state=1.code=11.token失效
                int responseCode = baseResponse.getResponseCode();
                String message = baseResponse.getResponseMsg()==null?"":baseResponse.getResponseMsg();
                if (state == 1) {
                    success(data);
                } else {
                    error(state, message);
                }
            }else {
                JSONObject jsonObject = (JSONObject) data;
                int state = jsonObject.getIntValue("state");
                int code = jsonObject.getIntValue("code");
                String msg = jsonObject.getString("msg")==null?"":jsonObject.getString("msg");
                if (state==1){
                    success(data);
                }else {
                    error(state, msg);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public abstract void success(T t);
    public abstract void error(int code, String message);
    @Override
    public void onComplete() {
    }

    @Override
    public void onSubscribe(Disposable d) {
    }


}
